const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
function debounce(fn, wait, immediate=false){
  var timeout
  var flag = true 
  // console.log("debounce111");
  
  var debounced =  function(){
    // console.log(arguments);
    
    // console.log("debounce2");
    // console.log(this);
    
    let that = this
    let args = arguments
    if(flag&&immediate){
      fn.apply(that,args)
      flag = false
      return 
    }
    if(timeout){
      clearTimeout(timeout)
    }
    timeout = setTimeout(()=>{ 
      if(flag){
        fn.apply(that,args)
      } 
      flag = true
    },wait)
  }


  debounced.cancel = function(){
    clearTimeout(timeout)
    timeout = null
  }
  return debounced
}
module.exports = {
  formatTime,
  debounce
}
